<?php

namespace app\common\util;

use app\common\model\AdminLog;
use app\common\model\BusinessAdminLog;
use think\facade\Request;

/**
 * 日志相关 工具类
 */
class LogHelper
{
    /**
     * 管理员日志
     * @param mixed $response
     * @param string $operationName
     * @return mixed
     */
    public static function logAdmin(mixed $response, string $operationName)
    {
        $request = Request::instance();

        $logData = [
            'admin_id' => AdminLoginInfo::getAdminId() ?? 0,
            'name' => $operationName,
            'content' => $request->getContent(),
            'url' => $request->url(true),
            'method' => $request->method(),
            'ip' => $request->ip(),
            'params' => json_encode($request->param()),
            'controller' => $request->controller(),
            'action' => $request->action(),
            'response' => json_encode($response, JSON_UNESCAPED_UNICODE)
        ];

        $log = AdminLog::create($logData);

        return $response;
    }

    /**
     * 企业管理员日志
     * @param mixed $response
     * @param string $operationName
     * @return mixed
     */
    public static function logBusinessAdmin(mixed $response, string $operationName)
    {
        $request = Request::instance();

        $logData = [
            'business_id' => BusinessAdminLoginInfo::getBusinessId() ?? 0,
            'admin_id' => BusinessAdminLoginInfo::getAdminId() ?? 0,
            'name' => $operationName,
            'content' => $request->getContent(),
            'url' => $request->url(true),
            'method' => $request->method(),
            'ip' => $request->ip(),
            'params' => json_encode($request->param()),
            'controller' => $request->controller(),
            'action' => $request->action(),
            'response' => json_encode($response, JSON_UNESCAPED_UNICODE)
        ];

        $log = BusinessAdminLog::create($logData);

        return $response;
    }
}
